<?php

use App\Admin\Controller\Base\AdministratorsController;
use App\Common\Enums\Base\AdministratorsEnumStatus;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

Html::create('管理员列表');

$table = Table::create(TP::route()->to([AdministratorsController::class, 'listsData'])->url(), 'administrators');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('用户名', 'name')->addSearch('like'),
    Table\Column::normal('账号', 'account')->addSearch('like'),
    Table\Column::normal('最近登录', 'lately_time'),
    Table\Column::normal('状态', 'status')->showSwitch(AdministratorsEnumStatus::mapping(),  TP::route()->to([AdministratorsController::class, 'switchToggle']))->addSearch(),
    Table\Column::normal('角色', 'role_ids')->setFormat('{{ role }}'),
    Table\Column::normal('创建时间', 'create_time'),
);


// ===================================================================
// 新页面: 默认为iframe模式, 当前使用 dialog-component
// ===================================================================
$table->setHeaderEvent("@primary.Refresh.刷新数据", "this.administratorsGetData()");

$component = Temporary::create('create')->setContent(include __DIR__ . '/form.sc.php');

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setConfig(['width' => '500px'])->setComponent($component)
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑')->setConfig(['width' => '500px'])->setComponent($component)
);
$table->setRowEvent('@primary.Refresh.重置密码',
    Table\EventHandler::post(TP::route()->to([AdministratorsController::class, 'reseatPassword']), ['id' => '@row.id'])
        ->confirmMessage('确认重置该用户密码吗？')
        ->success('this.$message.success("重置成功，新密码为：123456")')
);
$table->setRowEvent('@primary.User.登录',
    Table\EventHandler::post(TP::route()->to([AdministratorsController::class, 'loginAccount']), ['id' => '@row.id'])
        ->confirmMessage('确认登录该用户吗？')
        ->success(JsCode::make(
            JsCode::create('this.$message.success("登录成功");'),
            JsCode::create('setTimeout(() => top.window.location.reload(), 500)'),
        ))
);

//$table->setHeaderEvent('@primary.Plus.新增',
//    Table\EventHandler::window('新增')->setConfig(['width' => '500px'])->setUrl( TP::route()->to([AdministratorsController::class, 'create']))
//);
//
//$table->setRowEvent('@primary.Edit.编辑',
//    Table\EventHandler::window('编辑')->setConfig(['width' => '500px'])->setUrl( TP::route()->to([AdministratorsController::class, 'edit']), ['id' => '@id'])
//);
$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: TP::route()->to([AdministratorsController::class, 'destroy']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.administratorsGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);
return $table->render();
